import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    theme: "light", //主题切换名称
    user: null, //用户信息
    uid: null, //用户id
    cookie: null,
    isloading: false, //每个请求的加载loading
    requestErr: false, //请求错误提示
  },
  mutations: {
    // 设置主题的mutations
    setTheme(state, theme) {
      state.theme = theme;
      localStorage.setItem("theme", theme); //主题存储到localStorage
    },
    // 保存用户信息的mutations
    addUser(state, obj) {
      state.user = obj;
      state.uid = state.user.profile.userId;
      state.cookie = obj.cookie;
    },
    // 显示loading的mutations
    showLoading(state) {
      state.isloading = true;
    },
    // 隐藏loading的mutations
    hiddenLoading(state) {
      state.isloading = false;
    },
    // 修改网络状态
    setRequsetErr(state) {
      state.requestErr = true;
    },
  },
  getters: {
    getTheme(state) {
      //获取主题的getters
      //初始化主题
      if (localStorage.getItem("theme")) {
        state.theme = localStorage.getItem("theme");
      }
      return state.theme;
    },
    getAvatar(state) {
      //获取头像的getters
      let avatar = "";
      if (localStorage.getItem("avatar")) {
        avatar = localStorage.getItem("avatar");
        return avatar;
      }
      avatar = state.user && state.user.profile.avatarUrl;
      return avatar;
    },
    getUserId(state) {
      //获取用户id的getters
      if (localStorage.getItem("uid")) {
        state.uid = localStorage.getItem("uid");
      }
      return state.uid;
    },
  },
  actions: {},
  modules: {},
});
